package com.gulimall.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gulimall.product.entity.BrandEntity;
import com.gulimall.product.entity.CategoryEntity;
import com.gulimall.product.service.impl.BrandServiceImpl;
import com.gulimall.product.service.impl.CategoryBrandRelationServiceImpl;
import com.gulimall.product.service.impl.CategoryServiceImpl;
import com.gulimall.product.vo.BrandVoRes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.gulimall.product.entity.CategoryBrandRelationEntity;
import com.gulimall.product.service.CategoryBrandRelationService;
import com.common.utils.PageUtils;
import com.common.utils.R;

import javax.annotation.Resource;


/**
 * 品牌分类关联
 *
 * @author Rookie-886
 * @email ${email}
 * @date 2021-03-14 16:30:53
 */
@RestController
@RequestMapping("product/categorybrandrelation")
public class CategoryBrandRelationController {
    @Resource
    private CategoryBrandRelationServiceImpl categoryBrandRelationServiceImpl;
    @Resource
    private BrandServiceImpl brandServiceImpl;

    /**
     * 列表
     */
    @RequestMapping("/brands/list")
//    @RequiresPermissions("product:categorybrandrelation:list")
    public R list(@RequestParam Long catId){
        // 1、找出当前分组下所有品牌对象
        List<BrandEntity> brandEntityList = brandServiceImpl.queryBrandByCatId(catId);
        // 2、进行拼装，返回需要的对象集合
        List<BrandVoRes> brandVoResList = null;
        if(brandEntityList != null && brandEntityList.size() > 0){
            brandVoResList = brandEntityList.stream().map(brandEntity -> {
                BrandVoRes brandVoRes = new BrandVoRes();
                brandVoRes.setBrandId(brandEntity.getBrandId());
                brandVoRes.setBrandName(brandEntity.getName());
                return brandVoRes;
            }).collect(Collectors.toList());
        }
        return R.ok().put("data", brandVoResList);
    }

    /**
     * 查询关联分类列表
     */
    @GetMapping("/catelog/list")
//    @RequiresPermissions("product:categorybrandrelation:list")
    public R cateLogList(@RequestParam("brandId") Long brandId){
        List<CategoryBrandRelationEntity> list = categoryBrandRelationServiceImpl.list(new QueryWrapper<CategoryBrandRelationEntity>().eq("brand_id", brandId));
        return R.ok().put("data", list);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
//    @RequiresPermissions("product:categorybrandrelation:info")
    public R info(@PathVariable("id") Long id){
		CategoryBrandRelationEntity categoryBrandRelation = categoryBrandRelationServiceImpl.getById(id);

        return R.ok().put("categoryBrandRelation", categoryBrandRelation);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
//    @RequiresPermissions("product:categorybrandrelation:save")
    public R save(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){
        categoryBrandRelationServiceImpl.saveDetail(categoryBrandRelation);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
//    @RequiresPermissions("product:categorybrandrelation:update")
    public R update(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){
        categoryBrandRelationServiceImpl.updateById(categoryBrandRelation);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
//    @RequiresPermissions("product:categorybrandrelation:delete")
    public R delete(@RequestBody Long[] ids){
        categoryBrandRelationServiceImpl.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
